package com.example.pongv1;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;

import android.content.SharedPreferences;

import com.example.pongv1.BaseScene;
import com.example.pongv1.SceneManager.SceneType;

public class MyMenuScene extends BaseScene implements IOnMenuItemClickListener,Constantes{

	private MenuScene menuChildScene;
	private final int MENU_PLAY = 0;
	private final int MENU_OPTIONS = 1;
	private final int MENU_MULTI = 2;
	private final int MENU_INFO = 3;
	
	@Override
	public void createScene() {
		// TODO Auto-generated method stub
		createBackground();
		createMenuChildScene();
	}

	@Override
	public void onBackKeyPressed() {
		// TODO Auto-generated method stub
		System.exit(0);
		
	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.SCENE_MENU;
	}

	@Override
	public void disposeScene() {
		RessourcesManager.getInstance().unloadMenuTextures();
		
	}
	
	private void createBackground()
	{
	    attachChild(new Sprite(-CAMERA_WIDTH/2,-CAMERA_HEIGHT/2, ressourcesManager.menu_background_region, vbom));
	    
		
	}

	private void createMenuChildScene()
	{
	    menuChildScene = new MenuScene(camera);
	    menuChildScene.setPosition(400, 240);
	    
	    
	    final IMenuItem playMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_PLAY, ressourcesManager.play_region, vbom), 1.2f, 1);
	    final IMenuItem optionsMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_OPTIONS, ressourcesManager.options_region, vbom), 1.2f, 1);
	    final IMenuItem multiMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_MULTI, ressourcesManager.multi_region, vbom), 1.2f, 1);
	    final IMenuItem infoMenuItem = new ScaleMenuItemDecorator(new SpriteMenuItem(MENU_INFO, ressourcesManager.info_region, vbom), 1.2f, 1);
	    
	    menuChildScene.addMenuItem(playMenuItem);
	    menuChildScene.addMenuItem(multiMenuItem);
	    menuChildScene.addMenuItem(infoMenuItem);
	    menuChildScene.addMenuItem(optionsMenuItem);
	    
	    menuChildScene.buildAnimations();
	    menuChildScene.setBackgroundEnabled(false);
	    
	     playMenuItem.setPosition((menuChildScene.getX()+camera.getWidth())/2-playMenuItem.getWidth()-camera.getWidth()/2,playMenuItem.getY()-265);
	    optionsMenuItem.setPosition((menuChildScene.getX()+camera.getWidth())/2-playMenuItem.getWidth()-camera.getWidth()/2,optionsMenuItem.getY()-215);
	    multiMenuItem.setPosition((menuChildScene.getX()+camera.getWidth())/2-playMenuItem.getWidth()-camera.getWidth()/2,multiMenuItem.getY()-250);
	    infoMenuItem.setPosition((menuChildScene.getX()+camera.getWidth())/2-playMenuItem.getWidth()-camera.getWidth()/2,infoMenuItem.getY()-230);
	   
	    menuChildScene.setOnMenuItemClickListener(this);
	    
	    setChildScene(menuChildScene);
	    
	    // Restore from DB if sound is enabled.
	    SharedPreferences settings = activity.getSharedPreferences(ressourcesManager.PREFS_NAME, 0);
	       boolean issilent = settings.getBoolean("silentMode", true);
	       ressourcesManager.sound_activated=issilent;
	    if(ressourcesManager.sound_activated)
    	{ressourcesManager.music.play();
    	
    	}
    	else {ressourcesManager.music.pause();}
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		// TODO Auto-generated method stub
		 switch(pMenuItem.getID())
	        {
	        case MENU_PLAY:
	        	SceneManager.getInstance().loadGameScene(engine);
	            return true;
	        case MENU_OPTIONS:
	        	SceneManager.getInstance().loadOptionScene();
	            return true;
	        case MENU_INFO:
	        	SceneManager.getInstance().loadInfoScene();
	            return true;
	        case MENU_MULTI:
	        	
	            return true;
	        default:
	            return false;
	    }
	}
}
